package com.zwr.sheller.customer.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.WDMBusinessException;
import com.zwr.sheller.core.pojo.ShoppingCart;
import com.zwr.sheller.customer.Service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wdm
 */
@RestController
@RequestMapping("/shoppingCart")
@Api(value = "购物车模块")
public class ShoppingCartController {

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;

    @PostMapping("/selectShoppingCart")
    @ApiOperation(value = "查看购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer"),
    })
    public Map<String,Object> selectShoppingCart(Integer userId) throws WDMBusinessException {
        ShoppingCart shoppingCart = shoppingCartService.selectShoppingCart(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setField("Data",shoppingCart);
        return jsonResult.getMap();
    }

    @PostMapping("/updateShoppingCartSkuNum")
    @ApiOperation(value = "修改购物车中sku数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer"),
            @ApiImplicitParam(name = "skuId",value = "对应的skuId",dataType = "Long"),
            @ApiImplicitParam(name = "num",value = "修改后对应sku的数量",dataType = "Integer"),
    })
    public Map<String,Object> updateShoppingCartSkuNum(Integer userId, Long skuId, Integer num){
        shoppingCartService.updateShoppingCartSkuNum(userId, skuId, num);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }




    @PostMapping("/addShoppingCart")
    @ApiOperation(value = "商品添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer"),
            @ApiImplicitParam(name = "skuId",value = "商品对应的skuId",dataType = "Long"),
            @ApiImplicitParam(name = "num",value = "要添加进购物车的skuNum",dataType = "Integer"),
    })
    public Map<String,Object> addShoppingCart(Integer userId,Long skuId,Integer num) throws WDMBusinessException {
            shoppingCartService.addShoppingCart(userId, skuId, num);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();

    }

    @PostMapping("/deleteShoppingCart")
    @ApiOperation(value = "从购物车中删除部分商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer",paramType = "data"),
            @ApiImplicitParam(name = "skuIds",value = "要删除的skuId集合",dataType = "List<Long>",paramType = "data"),
    })
    public Map<String,Object> deleteShoppingCart(@RequestBody Map<String,Object> map){
        Integer userId =(Integer) map.get("userId");
        List<Long> skuIds =(List<Long>) map.get("skuIds");
        shoppingCartService.deleteShoppingCart(userId,skuIds);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("移出购物车成功");
        return jsonResult.getMap();


    }

    @PostMapping("/cleanShoppingCart")
    @ApiOperation(value = "清空购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer"),
    })
    public Map<String,Object> cleanShoppingCart(Integer userId) throws WDMBusinessException {
        shoppingCartService.cleanShoppingCart(userId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("清空购物车");
        return jsonResult.getMap();
    }
}
